package com.hexu.event.dao;

import com.hexu.event.entity.Administrator;
import com.hexu.event.entity.Attractions;
import com.hexu.event.util.JdbcUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class AdministratorDao {

    //添加景点
    public boolean addAttractions(Attractions attractions){
        String sql = "insert into attractions(name,open_time,ticket_fare,ticket_number,introduce,comment_number)"
                + " values (?,?,?,?,?,?);";
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn = JdbcUtil.getConn();
            ps = conn.prepareStatement(sql);
            //View层set的数据
            ps.setString(1, attractions.getName());
            ps.setString(2, attractions.getOpenTime());
            ps.setInt(3,attractions.getTicketFare());
            ps.setInt(4, attractions.getTicketNumber());
            ps.setString(5, attractions.getIntroduce());
            ps.setInt(6,attractions.getCommentNumber());
            ps.executeUpdate();

            //添加成功
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

            JdbcUtil.close(conn, ps, null);
        }

        //添加失败
        return false;
    }


    //删除景点
    public void deleteAttractions(String attractionsName){
        String sql = "delete from attractions where name = ? ";
        Connection conn = null;
        PreparedStatement ps = null;
        conn = JdbcUtil.getConn();
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1, attractionsName);
            ps.executeUpdate();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.close(conn, ps, null);
        }
    }


    //修改景点
    public boolean updateAttractions(Attractions attractions){
        String sql = "update attractions set open_time=?,ticket_fare=?,ticket_number=?,introduce=? where name=?";

        Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn = JdbcUtil.getConn();
            ps = conn.prepareStatement(sql);
            //View层set的数据
            ps.setString(1, attractions.getOpenTime());
            ps.setInt(2,attractions.getTicketFare());
            ps.setInt(3,attractions.getTicketNumber());
            ps.setString(4, attractions.getIntroduce());
            ps.setString(5, attractions.getName());
            ps.executeUpdate();

            //修改成功
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

            JdbcUtil.close(conn, ps, null);
        }

        //修改失败
        return false;
    }

    //返回所有管理员手机号码
    public List<Administrator> getAdministratorPhoneNumber(){
        List<Administrator> list = new ArrayList<>();
        String sql = "select * from administrator";
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = JdbcUtil.getConn();
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()){
                Administrator administrator = new Administrator();
                administrator.setPhoneNumber(rs.getString("phone_number"));
                list.add(administrator);
            }
            return list;

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            JdbcUtil.close(conn, ps, rs);
        }
        return null;
    }

    //返管理员数量
    public int administratorNumber(){
        String sql = "select phone_number from administrator ";
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = JdbcUtil.getConn();
            //使游标可滚动
            ps = conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
            rs = ps.executeQuery();

            // 将游标移动到最后一行上
            rs.last();
            //获取管理员数
            int administratorCount = rs.getRow();
            //把光标移至第一行前
            rs.beforeFirst();

            return administratorCount;

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            JdbcUtil.close(conn, ps, rs);
        }
        return 0;
    }



}
